package com.zretc.oasys.servlet;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.json.JSONObject;

import com.zret.facetools.Base64Util;
import com.zret.facetools.FaceBDTools;
import com.zret.facetools.GsonUtils;
import com.zret.face.entity.register.*;

/**
 * Servlet implementation class RegisterFace
 */
@WebServlet("/RegisterFace")
@MultipartConfig
public class RegisterFace extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterFace() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置请求编码
		request.setCharacterEncoding("utf-8");
		//获取用户id
		Object hashmapObject = request.getSession().getAttribute("emp");
		System.out.println(hashmapObject);
		HashMap hashMap=(HashMap)hashmapObject;
		String empid=hashMap.get("empid")+"";
		 
		//String empid = request.getParameter("empi");
		
		System.out.println("用户名："+empid);
		//获取文件
		Part imgfile = request.getPart("imgheading");
		//获取tomcat存放照片的路径
		String realPath = request.getRealPath("/empimgs/");
		//获取文件名
		String filename = imgfile.getSubmittedFileName();
		//拼接完整的路径,使用纳秒加文件名，避免文件名重复
		long serialnum = System.nanoTime();
		realPath=realPath+serialnum+filename;
		//System.out.println(realPath);
		//写入目标位置
		imgfile.write(realPath);
		//读取文件
		byte[] readAllBytes = Files.readAllBytes(Paths.get(realPath));
		//转换为base64格式
		String encodeStr64 = Base64Util.encode(readAllBytes);
		//调用人脸注册
		
		JSONObject rootJson = FaceBDTools.faceRegister(encodeStr64, empid);
		System.out.println(rootJson);
		//把返回的人脸注册的json转换成字符串格式
		String rootJsonStr = rootJson.toString();
		//可能会出现异常
		try {
			//使用GsonUtils给json实体类附加数据
			RegisterFaceRootBean jsonRootBean = GsonUtils.fromJson(rootJsonStr, RegisterFaceRootBean.class);
			//Error为0和error_msg 为SUCCESS的话，人脸添加成功
			//目前上传图片是简易版的上传注册人脸
			if (jsonRootBean.getError_code() == 0 && "SUCCESS".equals(jsonRootBean.getError_msg())) {
				//人脸添加成功的话，就跳转到主页面
				System.out.println("人脸注册成功");
				response.sendRedirect("UserInfo.jsp");
			} else {
				System.out.println("人脸注册失败");
				response.sendRedirect("UserInfo.jsp");
			} 
		} catch (Exception e) {
			//如果出现异常的话，也是重定向
			System.out.println("出现异常");
			response.sendRedirect("UserInfo.jsp");
		}
		
		
		
		
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
